#!/bin/bash

# This script makes nightly builds of Soot, Jasmin, Polyglot and other closely 
# related projects. There are several set up issues required to use it
# First you must set up an autobuild directory with three sub-directories 
# settings, releases and resources.
# In the settings directory put a soot and a jasmin base settings file
# This is used to derive the ant.settings file needed to build Soot and 
# Jasmin (one each)
# In the resources directory you need at least a copy of Eclipse.
# The Soot Eclipse plugin builds againts any 2.1.x version of Eclipse.
# You also need a sym link to the base nightly build html file. 
# The releases directory can be empty

rm -rf $HOME/autobuild/tmp;
rm -rf /tmp/autobuild;
mkdir /tmp/autobuild;
ln -s /tmp/autobuild $HOME/autobuild/tmp;
cd $HOME/autobuild/tmp;

#export CLASSPATH= 
export PATH=/usr/localcc/bin:$PATH 
#export JAVA_HOME=/usr/local/j2sdk1.4.2_01

polyglotrev=1.3.5;
rev=2.5.0;

# use this if building and using most recent polyglot
function getNightlyPolyglot(){
    # get polyglot from cvs
    cvs -Q -d :pserver:anoncvs@source.cs.cornell.edu:/cvsroot checkout polyglot || return;
}

#use this if using stable, released polyglot
function getReleasedPolyglot(){
    # get released polyglot from resources
    cp -r $HOME/autobuild/resources/polyglot-$polyglotrev-src $HOME/autobuild/tmp/polyglot || return;
}

# export jasmin and soot from subversion
function getJasminAndSoot(){
    # get jasmin, soot in autobuild/tmp dir
    svn export https://svn.sable.mcgill.ca/soot/jasmin/trunk jasmin > jasmin_export || return; 
    svn export -q https://svn.sable.mcgill.ca/soot/soot/trunk soot || return; 
}

function getJastAdd() {
    svn export -q http://svn.cs.lth.se/svn/jastadd-oxford/projects/trunk/JastAddJ
    svn export -q http://svn.cs.lth.se/svn/jastadd-oxford/projects/trunk/JastAddExtensions/
}

# set up date and revision tags
function getCurrentDate(){
    polyglotrev=`date +%Y%m%d` || return;
    echo "Date: $polyglotrev" || return;
}

# get current jasmin/soot revision
function getJasminSootRevision(){
    rev=`tail -1 jasmin_export | sed s/Exported' 'revision' '// | sed s/\\\.//` || return;
    rm jasmin_export || return;
    echo "Revision $rev" || return;
}

# build polyglot src jars
function buildPolyglotBarebones(){
    # switch to tmp dir
    cd $HOME/autobuild/tmp || return;

    # make a polyglot barebones jar and tar
    jar -cf $HOME/autobuild/releases/polyglotsrc-$polyglotrev.jar polyglot || return;
    tar -czf $HOME/autobuild/releases/polyglotsrc-$polyglotrev.tar.gz polyglot || return;

    echo "Built polyglot src packages." || return;
}

# build polyglot classes jars
function buildPolyglotClasses(){
    # switch to polyglot dir and build it first
    cd $HOME/autobuild/tmp/polyglot || return;

    if !(ant); then
        # if polyglot won't build send mail 
        # include date
        echo "Polyglot will not build $polyglotrev" | /usr/bin/mutt jlhotak@sable.mcgill.ca -e 'set from = jlhotak@sable.mcgill.ca' -s "Soot Automated Nightly Build" || return;
        echo "Polyglot will not build $polyglotrev" || return;
    else 
        # make polyglot jar out of classes and cup classes
        cd $HOME/autobuild/tmp/polyglot/classes || return; 
        jar -cf $HOME/autobuild/releases/polyglotclasses-$polyglotrev.jar * || return;
        cd $HOME/autobuild/tmp/polyglot/cup-classes || return;
        jar -uf $HOME/autobuild/releases/polyglotclasses-$polyglotrev.jar * || return;
    fi
}

# set up jasmin
function setUpJasmin(){
    # switch to jasmin dir and set cup variable to polyglot jar and build
    cd $HOME/autobuild/tmp/jasmin || return;
    # cp jasmin settings file
    sed s/cup-classes\\//autobuild\\/releases\\/polyglotclasses-$polyglotrev.jar/ $HOME/autobuild/settings/jasmin | sed s/2.2.1/$rev/ > ant.settings || return;
    
    # update version in source in jasmin 
    sed s/version' '=' '\"2.2.1\"/version' '=' '\"$rev\"/ src/jasmin/Main.java > version-temp || return;
    mv version-temp src/jasmin/Main.java || return;
}

# build the jasmin src jar and tar files
function buildJasminBarebones(){
    cd $HOME/autobuild/tmp/jasmin || return;
    ant barebones || return;
}
 
# build jasmin classes
function buildJasminClasses(){
    cd $HOME/autobuild/tmp/jasmin || return;
    if !(ant jasmin-jar); then
        # send mail if something is broken - add revision number
        echo "Jasmin will not build $rev" | /usr/bin/mutt jlhotak@sable.mcgill.ca -e 'set from = jlhotak@sable.mcgill.ca' -s "Soot Automated Nightly Build" || return;
        echo "Jasmin will not build $rev" || return;
    fi
}
 
# set up Soot
function setUpSoot(){
    # switch to soot
    cd $HOME/autobuild/tmp/soot || return;
    # cp soot settings file
    sed s/polyglotjar/autobuild\\/releases\\/polyglotclasses-$polyglotrev.jar/ $HOME/autobuild/settings/soot | sed s/jasminjar/autobuild\\/releases\\/jasminclasses-$rev.jar/ | sed s/sootversion/$rev/ > ant.settings || return;
       
    # update version in source in soot 
    sed s/versionString' '=' '\"2.2.1\"/versionString' '=' '\"$rev\"/ src/soot/Main.java > version-temp || return;
    mv version-temp src/soot/Main.java || return;
       
    # some care must be given to ensure the eclipse plugin.xml
    # file has the right jar file names for soot, jasmin and polyglot
    cd $HOME/autobuild/tmp/soot/eclipse/ca.mcgill.sable.soot/ || return;
    sed s/sootclasses-2.2.1-custom.jar/sootclasses-$rev.jar/ plugin.xml > temp1 || return;
    sed s/jasminclasses-2.2.1.jar/jasminclasses-$rev.jar/ temp1 | sed s/polyglotclasses-1.3.jar/polyglotclasses-$polyglotrev.jar/  > plugin.xml || return;
    rm temp1 || return;
}

# build soot src jars
function buildSootBarebones(){
    # build soot src jar and tar files
    cd $HOME/autobuild/tmp/soot || return;
    ant barebones || return;
}

# build soot classes
function buildSootClasses(){
    cd $HOME/autobuild/tmp/soot || return;
    if !(ant fulljar); then
        # if something is broken send mail
        echo "Soot will not build $rev" | /usr/bin/mutt ebodde@sable.mcgill.ca -e 'set from = ebodde@sable.mcgill.ca' -s "Soot Automated Nightly Build" || return;
        echo "Soot will not build $rev" || return;
    fi
}

# build soot javadoc
function buildSootJavaDoc(){
    cd $HOME/autobuild/tmp/soot || return;
    ant javadoc || return;
}

# build soot tutorials
function buildSootTutorials(){
    cd $HOME/autobuild/tmp/soot/tutorial || return;
    ./generate_tutorial || return;
    jar -cf $HOME/autobuild/releases/soot-tutorial-$rev.jar * || return;
    tar -czf $HOME/autobuild/releases/soot-tutorial-$rev.tar.gz * || return;
}

# make complete jar includes src and classes for soot, jasmin, polyglot
# includes src and classes for plugin and complete src and built tutorial
# by this point in the script all of soot, jasmin and polyglot are built
function buildComplete(){
    cd $HOME/autobuild/tmp || return;

    mv soot soot-$rev || return;
    mv jasmin jasmin-$rev || return;
    mv polyglot polyglot-$polyglotrev || return;

    jar -cf $HOME/autobuild/releases/sootall-$rev.jar soot-$rev jasmin-$rev polyglot-$polyglotrev || return;
    tar -czf $HOME/autobuild/releases/sootall-$rev.tar.gz soot-$rev jasmin-$rev polyglot-$polyglotrev || return;
}

# update nightly build html file
function updateNightlyHtml(){
    cd $HOME/autobuild/tmp/soot-$rev || return;
    sed s/sootall/sootall-$rev/g nightly.html > temp1.html || return;
    sed s/sootclasses/sootclasses-$rev/g temp1.html | sed s/jasminclasses/jasminclasses-$rev/g  | sed s/polyglotclasses/polyglotclasses-$polyglotrev/g | sed s/quickstart/soot-eclipse-quickstart-$rev/g | sed s/sootbarebones/sootsrc-$rev/g | sed s/jasminbarebones/jasminsrc-$rev/g  | sed s/polyglotbarebones/polyglotsrc-$polyglotrev/g > nightly.html || return;

    rm temp1.html || return;

    cp nightly.html $HOME/autobuild/releases/ || return;
    cd $HOME/autobuild/releases/ || return;
    chmod o+r nightly.html || return;
    chmod g+r nightly.html || return;
    cp nightly.html ~sable/public_html/soot/ || return;
    cp *$rev* ~sable/public_html/soot/nightly/ || return;
    cp *$polyglotrev* ~sable/public_html/soot/nightly/ || return;
    
}

function makeStable(){
    getReleasedPolyglot || exit;
    getJasminAndSoot || exit;
    getJastAdd || exit;
    buildPolyglotBarebones || exit;
    buildPolyglotClasses || exit;
    setUpJasmin || exit;
    buildJasminBarebones || exit;
    buildJasminClasses || exit;
    setUpSoot || exit;
    buildSootBarebones || exit;
    buildSootClasses || exit;
    buildSootJavaDoc || exit;
    buildSootTutorials || exit;
    buildComplete || exit;
#   updateNightlyHtml || exit;
}

function makeNightly(){
    getReleasedPolyglot || exit;
    getJasminAndSoot || exit;
    #getCurrentDate;
    getJasminSootRevision || exit;
    buildPolyglotBarebones || exit;
    buildPolyglotClasses || exit;
    setUpJasmin || exit;
    buildJasminBarebones || exit;
    buildJasminClasses || exit;
    setUpSoot || exit;
    buildSootBarebones || exit;
    buildSootClasses || exit;
    buildSootJavaDoc || exit;
    buildSootTutorials || exit;
    buildComplete || exit;
    updateNightlyHtml || exit;
}


makeStable
#makeNightly

#cd $HOME/autobuild &&

#cp settings/jasmin tmp/jasmin-trunk/ant.settings &&
#cp settings/jedd tmp/jedd-trunk/ant.settings &&
#cp settings/soot tmp/soot-trunk/ant.settings &&
#cp settings/paddle tmp/paddle-trunk/ant.settings &&
#cp settings/abc tmp/abc/ant.settings &&

#cd $HOME/autobuild/tmp &&
#cd jedd-trunk &&
#CLASSPATH=$HOME/autobuild/resources/cpptasks.jar ant &&
#cp $HOME/autobuild/tmp/jedd-trunk/runtime/lib/{jedd-runtime.jar,libjeddbuddy.so,libjeddcudd.so} $HOME/autobuild/releases &&
#cp $HOME/autobuild/tmp/jedd-trunk/translator/lib/jedd-translator.jar $HOME/autobuild/releases &&

#cd $HOME/autobuild/tmp &&
#cd jasmin-trunk &&
#ant &&
#cd $HOME/autobuild/tmp/jasmin-trunk/classes &&
#jar cf $HOME/autobuild/releases/jasminclasses.jar . &&

#( scp musk:/usr/local/src/polyglot-dev/lib/polyglot.jar $HOME/autobuild/resources && 
#scp musk:/usr/local/src/polyglot-dev/lib/java_cup.jar $HOME/autobuild/resources &&
#scp musk:/usr/local/src/xact/java/xact-complete.jar $HOME/autobuild/resources &&
#cd $HOME/autobuild/tmp &&
#mkdir polyglot &&
#cd $HOME/autobuild/tmp/polyglot &&
#jar xf $HOME/autobuild/resources/polyglot.jar &&
#jar xf $HOME/autobuild/resources/java_cup.jar &&
#jar cf $HOME/autobuild/resources/polyglot-cvs.jar .
#) ;

#cd $HOME/autobuild/tmp &&
#cd soot-trunk &&
#ant classesjar &&
#cd systests &&
#CLASSPATH=$HOME/autobuild/tmp/soot-trunk/classes:$HOME/autobuild/tmp/jasmin-trunk/classes:$HOME/autobuild/resources/polyglot-cvs.jar:.:/usr/localcc/pkgs/j2sdk1.4.2/jre/lib/rt.jar ./go &&

#cd $HOME/autobuild/tmp &&
#cd paddle-trunk &&
#ant classesjar &&

#cd $HOME/autobuild/tmp &&
#cd abc &&
#ant &&
#ant paddle-jar &&
#cp $HOME/autobuild/tmp/abc/lib/*.jar $HOME/autobuild/releases &&

#cd $HOME/autobuild/tmp &&
#cd abc &&
#cd ajc-harness &&
#cd tests &&
#export CLASSPATH=$HOME/autobuild/releases/abc.jar:$HOME/autobuild/releases/abc-testing.jar:$HOME/autobuild/releases/abc-runtime.jar:$HOME/autobuild/releases/sootclasses-nightly.jar:$HOME/autobuild/resources/polyglot-cvs.jar:$HOME/autobuild/releases/jasminclasses.jar &&
#java -Xmx768M -Dabc.home=$HOME/autobuild/tmp/abc abc.testing.Main abcTests.xml &&
#cd $HOME/autobuild/tmp &&
#cd abc &&
#cd ajc-harness &&
#cd tests &&
#./calc_updates.pl &&

#cd $HOME/autobuild/tmp &&
#cd abc &&
#cd ajc-harness &&
#cd tests &&
#export CLASSPATH=$HOME/debugabc/:$HOME/autobuild/releases/abc.jar:$HOME/autobuild/releases/abc-testing.jar:$HOME/autobuild/releases/abc-runtime.jar:$HOME/autobuild/releases/sootclasses-nightly.jar:$HOME/autobuild/resources/polyglot-cvs.jar:$HOME/autobuild/releases/jasminclasses.jar &&
#java -Xmx768M -Dabc.home=$HOME/autobuild/tmp/abc abc.testing.Main abcTests.xml &&
#cd $HOME/autobuild/tmp &&
#cd abc &&
#cd ajc-harness &&
#cd tests &&
#./calc_updates.pl | $HOME/bin/mutt abc@comlab.ox.ac.uk -e 'set from = olhotak@sable.mcgill.ca' -c olhotak@sable.mcgill.ca -s "test run results (with re-weaving)" &&

#true;
